<!--
@name:riskAssessmentDialog
@author:Zhenglei
@date:11:27
@description:riskAssessmentDialog
@update:2022/7/2911:27
-->
<template>
  <el-dialog title="风险评估"
             :visible.sync="visible"
             :before-close="modalClose"
             v-loading="loading"
             :close-on-click-modal="false"
             :close-on-press-escape="false"
             width="60%">
    <element-table class="mt-20"
                   :columns="columns"
                   :data="list">

      <template v-slot:rank="{row}">
        <el-select v-model="row.rank"
                   placeholder="请选择风险等级">
          <el-option v-for="item in riskDataSources"
                     :key="item.id"
                     :label="item.name"
                     :value="item.id">
          </el-option>
        </el-select>

      </template>

    </element-table>
    <p align="center">
      <el-button @click="modalClose"
                 type="info">取 消
      </el-button>
      <el-button type="primary"
                 v-if="list.length>0"
                 v-prevent-re-click
                 @click="handleSave">保 存
      </el-button>
    </p>

  </el-dialog>
</template>

<script>
import ElementTable from "../../../../base/ElementTable";

export default {
  name: "riskAssessmentDialog",
  props: {
    dataObj: {
      type: Object,
      default: {}
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      loading: false,
      list: [],
      riskDataSources: [{name: '低风险', id: 1}, {name: '中风险', id: 2}, {name: '高风险', id: 3}],
      columns: [
        {
          label: '量表名称',
          prop: 'scaleName',
          align: 'center'
        }, {
          label: '量表结论',
          prop: 'resultName',
          align: 'center'
        }, {
          label: '风险等级',
          prop: 'rank',
          align: 'center',
          isSlot: true
        }
      ]
    }
  },
  created() {
    this.getRiskAssessmentDetail()
  },
  methods: {
    modalClose() {
      this.$emit('update:visible', false); // 直接修改父组件的属性
    },
    //获取详情
    getRiskAssessmentDetail() {
      this.post(this.api.healthMap.getRiskAssessmentDetail, {
        id: this.dataObj.id
      }).then((res) => {
        if (res.data.code === '200') {
          this.list = res.data.object.riskAssessments
        }
      })
    },
    handleSave() {
      let list = this.list
      if (list.every(item => item.rank)) {
        this.post(this.api.healthMap.saveRiskAssessment, {
          id: this.dataObj.id,
          riskAssessments: list
        }).then((res) => {
          if (res.data.code === '200') {
            this.$message.success('保存成功')
            this.modalClose()
            this.$parent.getList()
          }
        })
      } else {
        this.$message.warning('请选择风险等级')
      }
    },
  },
  components: {
    'element-table': ElementTable,
  }
}
</script>

<style scoped>

</style>
